package org.ucas.faker.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.ucas.faker.web.dto.CarryDto;
import org.ucas.faker.web.dto.ListDto;
import org.ucas.faker.web.dto.PatentDto;
import org.ucas.faker.web.dto.ResponseDto;
import org.ucas.faker.web.entity.patents.Patent;
import org.ucas.faker.web.exceptions.BadRequestException;

import java.lang.reflect.Field;
import java.util.*;

@RestController
@SuppressWarnings("unchecked")
public class PreferenceController extends BaseController {

  @RequestMapping(value = "/preferences/patent/fields", method = RequestMethod.GET)
  ListDto<String> patentFields() {
    return ListDto.of(context().preference.patentFields);
  }

  @RequestMapping(value = "/preferences/patent/fields", method = RequestMethod.PUT)
  ResponseDto updatePatentFields(@RequestParam(value = "fields", required = true) List<String> fields) {
    if (fields == null) throw BadRequestException.of("Invalid request parameter fields!");
    Set<String> cols = new HashSet<>();
    for (Field f : Patent.class.getDeclaredFields()) cols.add(f.getName());
    for (String field : fields)
      if (!cols.contains(field)) throw BadRequestException.of("Invalid patent field selected!");
    context().preference.patentFields = fields;
    return ResponseDto.OK;
  }

  @RequestMapping(value = "/preferences/graph/selectd", method = RequestMethod.GET)
  ListDto<String> selectedGraphs() {
    return ListDto.of(context().preference.graphCodes);
  }

  @RequestMapping(value = "/preferences/graph/selected", method = RequestMethod.PUT)
  ResponseDto updateSelectedGraph(@RequestParam(value = "codes", required = true) List<String> codes) {
    if (codes == null) throw BadRequestException.of("Invalid request parameter graph codes!");
    context().preference.graphCodes = codes;
    return ResponseDto.OK;
  }


  @RequestMapping(value = "/table/set/columns", method = RequestMethod.POST)
  public ResponseDto setShowColumns(@RequestParam(value = "ids", required = false) String ids) {

//    List<PatentDto> state0s = searchResult0();
    JSONArray jsonArr = JSON.parseArray(ids);
    List<String> addIDS = new ArrayList<>();
    for (int i = 0; i < jsonArr.size(); i++) {
      addIDS.add(jsonArr.getString(i));
    }
    columns(addIDS);
    return ResponseDto.OK;
  }

  @RequestMapping(value = "/table/get/columns", method = RequestMethod.GET)
  public CarryDto<String> getShowColumns() {
    if (columns() == null) {
      List<String> n = new ArrayList<String>();
      return CarryDto.of(n);
    }
    return CarryDto.of(columns());
  }

  @RequestMapping(value = "/table/get/keywords", method = RequestMethod.GET)
  public CarryDto<String> getKeyWords() {
    if (keywords() == null) {
      List<String> n = new ArrayList<String>();
      return CarryDto.of(n);
    }
    return CarryDto.of(keywords());
  }

  @RequestMapping(value = "/table/set/keywords", method = RequestMethod.POST)
  public ResponseDto setKeyWords(@RequestParam(value = "searchQuery", required = false) String queryString) {

    if (queryString == null || queryString.length() == 0) {
    } else {
      String[] t = queryString.split("\"");
      List<String> n = new ArrayList<>();
      for (int i = 0; i < t.length; i++) {
        if (i % 2 == 1) {
          n.add(t[i]);
        }
      }
      keywords(n);

    }
    return ResponseDto.OK;
  }
  @RequestMapping(value = "/graph/draw/test", method = RequestMethod.POST)
  public CarryDto<String> graphDrawTest(@RequestParam(value = "graph_message", required = true) String graphMessage) {

    List<Double> xAxisData = new ArrayList<>();
    List<Double> data1 = new ArrayList<>();
    List<Double> data2 = new ArrayList<>();
    for (double i = 0; i < 100; i++) {
        xAxisData.add(i);
        data1.add((Math.sin(i / 5) * (i / 5 -10) + i / 6) * 5);
        data2.add((Math.cos(i / 5) * (i / 5 -10) + i / 6) * 5);
    }
    String option = "{"+
    	    "title: {"+
    	        "text: '柱状图动画延迟'"+
    	    "},"+
    	    "legend: {"+
    	        "data: ['申请', '授权'],"+
    	        "align: 'left'"+
    	    "},"+
    	    "toolbox: {"+
    	        // y: 'bottom',
    	        "feature: {"+
    	            "magicType: {"+
    	                "type: ['stack', 'tiled']"+
    	            "},"+
    	            "dataView: {},"+
    	            "saveAsImage: {"+
    	                "pixelRatio: 2"+
    	            "}"+
    	        "}"+
    	    "},"+
    	    "tooltip: {},"+
    	    "xAxis: {"+
    	        "data:"+xAxisData+","+
    	        "silent: false,"+
    	        "splitLine: {"+
    	            "show: false"+
    	        "}"+
    	    "},"+
    	    "yAxis: {"+
    	    "},"+
    	    "series: [{"+
    	        "name: '申请',"+
    	        "type: 'bar',"+
    	        "data: "+data1+","+
    	        "animationDelay: function (idx) {"+
    	            "return idx * 10;"+
    	        "}"+
    	    "}, {"+
    	        "name: '授权',"+
    	        "type: 'bar',"+
    	        "data: "+data2+","+
    	        "animationDelay: function (idx) {"+
    	            "return idx * 10 + 100;"+
    	        "}"+
    	    "}],"+
    	    "animationEasing: 'elasticOut',"+
    	    "animationDelayUpdate: function (idx) {"+
    	        "return idx * 5;"+
    	    "}"+
    	"}";
    return CarryDto.of(option);
  }
}
